package com.s.rpc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ShenshenLuo
 * @email luoshenshen@buaa.edu.cn
 * @date 2025/5/11 13:49
 **/
public class ServiceRegistry {
    private final Map<String, Object> services = new ConcurrentHashMap<>();

    public void register(Object service) {
        Class<?>[] interfaces = service.getClass().getInterfaces();
        if (interfaces.length == 0) {
            throw new IllegalArgumentException("Service must implement at least one interface");
        }
        String serviceName = interfaces[0].getName();
        services.put(serviceName, service);
        System.out.println("Registered service " + serviceName);
    }

    public Object getService(String serviceName) {
        Object service = services.get(serviceName);
        if (service == null) {
            throw new IllegalArgumentException("Service not found: " + serviceName);
        }
        return service;
    }
}
